﻿;(function(){
  'use strict';
  angular.module('controllers.workOrderData.developWorks')
  .controller('worksComparedController',worksComparedController);
  function worksComparedController($scope,$filter,$timeout,workOrderDataService,worksComparedService,CoreService,CoreLoading,PublicFn,COREDATA){
  	$scope.timeTypes = worksComparedService.getTimeTypes();
  	$scope.flag = {
  		state: true,
  		arrow: false,
  		show: 'view'
  	};
  	//导出路径
  	$scope.formPrefix = COREDATA.FORMPREFIX+'workOrderData/developWorks/export';
  	var date = new Date();
  	var year = date.getFullYear();
  	var month = date.getMonth();
  	var day = date.getDate();
  	$scope.send = {};
  	//页面查询项绑定对象
  	$scope.params = {
  		timeType: '3',
  		startTime: new Date(year,month-1),
  		endTime: new Date(year,month-1),
  		changeTypeCode: '1'
  	};
	//监听日期
  	$scope.$watch('params.timeType',function(newValue){
  		switch (newValue) {
			case '5':
				$scope.params.startTime = year-1;
				$scope.params.endTime = year-1;
				break;
			case '4':
				$scope.params.startTime = new Date(year,month-3);
				$scope.params.endTime = new Date(year,month-3);
				break;
			case '3':
				$scope.params.startTime = new Date(year, month-1);
				$scope.params.endTime = new Date(year, month-1);
				break;
		}
  	});
	var myEcharts = echarts.init(document.getElementById('doc'));
  	//查询
  	$scope.search = function(){
  		var changeTypeName = $scope.params.changeTypeCode === '1' ? '分系统工作量分析' : '分开发者工作量分析';
  		$scope.changeType = $scope.params.changeTypeCode === '1' ? '业务系统' : '开发者';
  		$scope.flag.state = !$scope.flag.state;
  		if(!$scope.flag.state){
  			switch ($scope.params.timeType) {
					case '3':
						$scope.send.startTime = CoreService.dateFormat($scope.params.startTime, 'yyyy-MM-dd');
						$scope.send.endTime = CoreService.dateFormat($scope.params.endTime, 'yyyy-MM-dd');
						break;
					case '4':
						var start = worksComparedService.splitTime($scope.params.startTime);
						var end = worksComparedService.splitTime($scope.params.endTime);
						$scope.send.startTime = $scope.params.startTime.substring(0, 4)+'-01-01';
						$scope.send.startQuarter = start.quarter;
						$scope.send.endTime = $scope.params.endTime.substring(0, 4)+'-01-01';
						$scope.send.endQuarter = end.quarter;
						break;
					case '5':
						$scope.send.startTime = $scope.params.startTime + '-01-01';
						$scope.send.endTime = $scope.params.endTime + '-01-01';
						break;
				}
	  		var timeTitle = worksComparedService.fmtDateTitle($scope.send,$scope.params.timeType);
				$scope.params.title =  timeTitle + changeTypeName;
				$scope.send = {
					timeType : $scope.params.timeType,
					changeTypeCode : $scope.params.changeTypeCode,
					title : $scope.params.title,
					startTime : $scope.send.startTime,
					endTime : $scope.send.endTime,
					startQuarter : $scope.send.startQuarter,
					endQuarter : $scope.send.endQuarter
				};
	  		worksComparedService.query($scope.send,function(res){
				if(res.state === '10000' && res.data.length >= 1){
					myEcharts.clear();
					$scope.items = res.data;
					$scope.title = $scope.send.title;
					var option = worksComparedService.getEchartsInstance(res,$scope.title,'filedName','percentage',changeTypeName);
					myEcharts.setOption(option);
					$scope.outData = workOrderDataService.exportView(myEcharts,$scope.send);
					$scope.flag.arrow = true;
					$scope.flag.show = 'view';
				}else if(res.data.length === 0){
						workOrderDataService.queryNoDataOrError($scope.flag,'查询无数据');
				}else {
						workOrderDataService.queryNoDataOrError($scope.flag,'查询错误');
				}
				CoreLoading.setState(false);
			});
		}
  	};
			$scope.search();
		//监听表单,当表单有错时,禁止查询
		$scope.$watch('myForm.$invalid', function(newValue) {
			newValue ? $scope.flag.arrow = false : $scope.flag.arrow = true;
		});
  }
   worksComparedController.$inject = ['$scope','$filter','$timeout','workOrderDataService','worksComparedService','CoreService','CoreLoading','PublicFn','COREDATA'];
})();